package com.tz.farmer.controller;


import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.service.DataOfShareService;
import com.tz.farmer.service.DoorService;
import com.tz.farmer.so.BusinessPushSo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @description: 台农/渔门户
 * @name BusinessController
 * @author: xhj
 * @date: 2021/6/23
 * @version V1.0
 */
@RestController
@RequestMapping("door")
@Api(value = "台农/渔门户", tags = "台农/渔门户控制器")
public class DoorController {

    private static Logger logger = LoggerFactory.getLogger(DoorController.class);

    @Resource
    DoorService doorService;
    @Resource
    DataOfShareService dataOfShareService;

    /**
     * @description:  查询门户统计信息
     * @name businessInfo
     * @author: xhj
     * @date: 2021/8/6 11:35
     * @version V1.0
     */
    @ApiOperation("查询门户统计信息")
    @GetMapping("statisticsInfo")
    public ResultEx getStatisticsInfo() {
        try {
            return doorService.getStatisticsInfo();
        }catch (Exception e){
            logger.error("统计信息获取失败",e);
            return ResultEx.builder().makeFailedResult("统计信息获取失败");

        }
    }

    /**
     * @description:  查询共享清单
     * @name businessInfo
     * @author: xhj
     * @date: 2021/8/6 11:35
     * @version V1.0
     */
    @ApiOperation("查询共享清单")
    @GetMapping("shareInfo")
    public ResultEx getShareInfo() {
        try {
            return doorService.getShareInfo();
        }catch (Exception e){
            logger.error("共享清单信息获取失败",e);
            return ResultEx.builder().makeFailedResult("共享清单信息获取失败");

        }
    }



    /**
     * @description:  下载政策体系
     * @name instrucDownLoad
     * @author: xhj
     * @date: 2021/8/9 12:48
     * @version V1.0
     * @param fileName:
     * @return: com.nokia.xnzx.msframe.baseobj.result.ResultEx
     */
    @GetMapping("downLoad")
    @ApiOperation("下载政策体系")
    public void downLoad(String fileName, HttpServletResponse response) throws IOException {
        OutputStream out = null;
        FileInputStream in=null;
        try {
            File file=new File("/opt/smartPark/file/farmerdoor/"+fileName+".docx");
            fileName=URLEncoder.encode(fileName,"UTF-8");
            in = new FileInputStream(file);
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/msword");
            response.addHeader("Content-Disposition","attachment;filename="+fileName+".docx");
            out = response.getOutputStream();
            byte[] buffer = new byte[1024]; // 缓冲区
            int bytesToRead = -1;
            // 通过循环将读入的Word文件的内容输出到浏览器中
            while ((bytesToRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
        } finally {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        }
    }


    /**
     * kai H5展示
     * @return
     */
    @GetMapping("/three")
    @ApiOperation("查询")
    public ResultEx getDataOfShare(){
        Map dataOfShare = dataOfShareService.getDataOfShare();
        return ResultEx.builder().makeSuccessResult(dataOfShare);
    }




}
